package com.example.juc.controller;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.LongAdder;

/**
 * @author jl
 * @since 2021/10/10 15:29
 */
public class LongAdderCalcDemo {

    int num = 0;

    public synchronized void add_synchronized() {
        num++;
    }

    AtomicInteger atomicInteger = new AtomicInteger();

    public void add_AtomicInteger() {
        atomicInteger.incrementAndGet();
    }

    LongAdder longAdder = new LongAdder();

    public void add_LongAdder() {
        longAdder.increment();
    }

    public static void main(String[] args) throws InterruptedException {
        int count = 50;
        int size = 1000000;
        LongAdderCalcDemo demo = new LongAdderCalcDemo();
        CountDownLatch countDownLatch1 = new CountDownLatch(count);
        CountDownLatch countDownLatch2 = new CountDownLatch(count);
        CountDownLatch countDownLatch3 = new CountDownLatch(count);
        long start;
        long end;

        start = System.currentTimeMillis();
        for (int i = 0; i < count; i++) {
            new Thread(() -> {
                try {
                    for (int j = 0; j < size; j++) {
                        demo.add_synchronized();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    countDownLatch1.countDown();
                }
            }).start();
        }
        countDownLatch1.await();
        end = System.currentTimeMillis();
        System.out.println("add_synchronized costTime = " + (end - start) + " 毫秒, 结果 = " + demo.num);

        start = System.currentTimeMillis();
        for (int i = 0; i < count; i++) {
            new Thread(() -> {
                try {
                    for (int j = 0; j < size; j++) {
                        demo.add_AtomicInteger();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    countDownLatch2.countDown();
                }
            }).start();
        }
        countDownLatch2.await();
        end = System.currentTimeMillis();
        System.out.println("add_AtomicInteger costTime = " + (end - start) + " 毫秒, 结果 = " + demo.atomicInteger.get());

        start = System.currentTimeMillis();
        for (int i = 0; i < count; i++) {
            new Thread(() -> {
                try {
                    for (int j = 0; j < size; j++) {
                        demo.add_LongAdder();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    countDownLatch3.countDown();
                }
            }).start();
        }
        countDownLatch3.await();
        end = System.currentTimeMillis();
        System.out.println("add_LongAdder costTime = " + (end - start) + " 毫秒, 结果 = " + demo.longAdder.sum());
    }
}
